package framework.graphics.opengl;

import javax.microedition.khronos.opengles.GL10;

import framework.maths.Vector2;

/**
 * TODO Otimizar.
 *
 */
public class Camera2D {
    public final Vector2 position;//posição da camera
    public float zoom; // zoom
    public final float frustumWidth;//largura
    public final float frustumHeight;//altura
    final GLGraphics glGraphics;

    public Camera2D(GLGraphics glGraphics, float frustumWidth, float frustumHeight) {
        this.glGraphics = glGraphics;
        this.frustumWidth = frustumWidth;
        this.frustumHeight = frustumHeight;
        this.position = new Vector2(0, 0);
        this.zoom = 1.0f;
    }
    
    /**
     * Ajusta a camera
     */
    public void setViewportAndMatrices() {
        GL10 gl = glGraphics.getGL();
        gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(position.x , 
                    position.x + frustumWidth * zoom, 
                    position.y , 
                    position.y + frustumHeight * zoom, 
                    1, -1);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

	/*
	 * Converte de coordenadas da tela, para coordenadas do mundo 
	 */
    public void touchToWorld(Vector2 touch) {
        touch.x = (touch.x / (float) glGraphics.getWidth()) * frustumWidth * zoom;
        touch.y = (1 - touch.y / (float) glGraphics.getHeight()) * frustumHeight * zoom;
    }
}
